package com.huifaapp.mall.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.GoodsListXRVAdapter;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.GoodsClassifyListData;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DM on 2018/1/29.
 */
@SuppressLint("ValidFragment")
public class TmallClassifyWithSPBJ extends TmallClassifyBaseFragment{
    private Context context;
    private View view;
    private XRecyclerView jkbjXRV;
    private List<GoodsClassifyListData.DataBean.ListBean> listBeanList = new ArrayList<>();
    private GoodsListXRVAdapter goodsListXRVAdapter;
    private String categroyId;
    private int start = 0;
    private int lastOffset;
    private int lastPosition;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_jkbj,container,false);
        initView();
        initAdapter();
        initData(start);
        return view;
    }

    private void initData(int start) {

        String url = ApiManager.BASE_NIUPAI_URL+"goods/list";
        OkHttpUtils
                .get()
                .url(url)
                .addParams("categoryId",categroyId)
                .addParams("start",start+"")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Log.e("分页数据",response);
                        Gson gson = new Gson();
                        GoodsClassifyListData goodsClassifyListData =
                                gson.fromJson(response,GoodsClassifyListData.class);
                        List<GoodsClassifyListData.DataBean.ListBean> goods =
                                goodsClassifyListData.getData().getList();
                        for(GoodsClassifyListData.DataBean.ListBean good: goods)
                        {
                            listBeanList.add(good);
                        }
                        goodsListXRVAdapter.getListBeans().addAll(listBeanList);
                        goodsListXRVAdapter.notifyDataSetChanged();

                    }
                });

    }

    private void initAdapter() {
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        jkbjXRV.setLayoutManager(staggeredGridLayoutManager);
        goodsListXRVAdapter =
                new GoodsListXRVAdapter(getActivity());
        jkbjXRV.setAdapter(goodsListXRVAdapter);
        jkbjXRV.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //这里执行下拉刷新逻辑
                goodsListXRVAdapter.getListBeans().clear();
                initData(start = 0);
            }

            @Override
            public void onLoadMore() {
                //这里执行上拉加载逻辑
                initData(++start);
                scrollToPosition();
            }
        });
        //监听RecyclerView滚动状态
        jkbjXRV.addOnScrollListener(new XRecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(recyclerView.getLayoutManager() != null) {
                    getPositionAndOffset();
                }
            }
        });
    }
    /**
     * 记录RecyclerView当前位置
     */
    private void getPositionAndOffset() {
        StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) jkbjXRV.getLayoutManager();
        //获取可视的第一个view
        View topView = layoutManager.getChildAt(0);
        if(topView != null) {
            //获取与该view的顶部的偏移量
            lastOffset = topView.getTop();
            //得到该View的数组位置
            lastPosition = layoutManager.getPosition(topView);
        }
    }
    /**
     * 让RecyclerView滚动到指定位置
     */
    private void scrollToPosition() {
        if(jkbjXRV.getLayoutManager() != null && lastPosition >= 0) {
            ((StaggeredGridLayoutManager) jkbjXRV.getLayoutManager()).scrollToPositionWithOffset(lastPosition, lastOffset);
        }
    }
    private void initView() {
        jkbjXRV = (XRecyclerView) view.findViewById(R.id.jkbj_xrv);

    }
}
